package com.wnnight.wys.web.role;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wnnight.wys.entity.role.MenuPerm;
import com.wnnight.wys.entity.role.Role;
import com.wnnight.wys.entity.role.RoleMenuMid;
import com.wnnight.wys.execlListener.MenuPermExcelListener;
import com.wnnight.wys.groupInterface.AddGroup;
import com.wnnight.wys.groupInterface.AlterGroup;
import com.wnnight.wys.service.role.MenuPermService;
import com.wnnight.wys.service.role.RoleMenuMidService;
import com.wnnight.wys.service.role.RoleService;
import com.wnnight.wys.vo.ResultVo;
import com.wnnight.wys.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: RoleController
 * @Description: //TODO
 * @author: dz
 * @date: 2022-08-31-15:25
 */
@RestController
@RequestMapping("/api/v1/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuPermService menuPermService;

    @Autowired
    private RoleMenuMidService roleMenuMidService;

    @PostMapping("/roleMenu/upload")
    public ResultVo<?> upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), MenuPerm.class, new MenuPermExcelListener(menuPermService)).sheet().doRead();
        return ResultVo.SUCCESS;
    }

    /**
     * 可以用于产生列表和产生树
     *
     * @return
     */
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('wy:role:list')")
    public ResultVo<?> list() {
        List<Role> roles = roleService.list();
        return new ResultVo<>(roles);
    }

    /**
     * 修改权限菜单
     *
     * @param menuPerm
     * @return
     */
    @PostMapping("/menuPerm/update")
    @PreAuthorize("hasAuthority('wy:role:menuperm:update')")
    public ResultVo<?> updateMenuPerm(@RequestBody MenuPerm menuPerm) {
        menuPermService.updateById(menuPerm);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/menuPerm/list")
    @PreAuthorize("hasAuthority('wy:role:menuperm:list')")
    public ResultVo<?> getMenuPerm() {
        return new ResultVo<>(menuPermService.list());
    }

    @PostMapping("/menuPerm/add")
    @PreAuthorize("hasAuthority('wy:role:menuperm:add')")
    public ResultVo<?> addMenuPerm(@RequestBody MenuPerm menuPerm) {
        menuPermService.save(menuPerm);
        return ResultVo.SUCCESS;
    }

    /**
     * 通过角色id查找权限列表，用于权限树的选择（？选择返回权限id的数组集合）
     *
     * @param roleId
     * @return
     */
    @PostMapping("/getPermByRoleId")
    @PreAuthorize("hasAuthority('wy:role:menuperm:list')")
    public ResultVo<?> getPermByRoleId(int roleId) {
        return new ResultVo<>(menuPermService.getByRoleId(roleId));
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('wy:role:add')")
    public ResultVo<?> add(@RequestBody @Validated(AddGroup.class) RoleVo roleVo) {
        roleService.save(roleVo.getRole());
        System.out.println(roleVo);
        QueryWrapper<RoleMenuMid> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleVo.getRole().getId());
        roleMenuMidService.remove(wrapper);
        List<RoleMenuMid> roleMenuMidList = new ArrayList<>();
        for (int i : roleVo.getMenuPermIds()) {
            RoleMenuMid roleMenuMid = new RoleMenuMid();
            roleMenuMid.setRoleId(roleVo.getRole().getId());
            roleMenuMid.setMenuId(i);
            roleMenuMidList.add(roleMenuMid);
        }
        roleMenuMidService.saveBatch(roleMenuMidList);
        return new ResultVo<>(roleVo.getRole().getId());
    }

    @PostMapping("/update")
    @Transactional
    @PreAuthorize("hasAuthority('wy:role:update')")
    public ResultVo<?> update(@RequestBody @Validated(AlterGroup.class) RoleVo roleVo) {
        roleService.updateById(roleVo.getRole());
        QueryWrapper<RoleMenuMid> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleVo.getRole().getId());
        roleMenuMidService.remove(wrapper);
        List<RoleMenuMid> roleMenuMidList = new ArrayList<>();
        for (int i : roleVo.getMenuPermIds()) {
            RoleMenuMid roleMenuMid = new RoleMenuMid();
            roleMenuMid.setRoleId(roleVo.getRole().getId());
            roleMenuMid.setMenuId(i);
            roleMenuMidList.add(roleMenuMid);
        }
        roleMenuMidService.saveBatch(roleMenuMidList);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/delete")
    @Transactional
    @PreAuthorize("hasAuthority('wy:role:delete')")
    public ResultVo<?> delete(int roleId) {
        QueryWrapper<RoleMenuMid> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        roleMenuMidService.remove(wrapper);
        roleService.removeById(roleId);
        return ResultVo.SUCCESS;
    }
}
